package docker

import (
	"context"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/filters"
	"github.com/docker/docker/api/types/network"
	"github.com/docker/docker/client"
)

// var DockerNetwork = &dockerNetwork{
// }

type dockerNetwork struct {
}

func (receiver *dockerNetwork) Create(networkName string) (types.NetworkCreateResponse, error) {
	cli, err := Docker.NewClient()
	if err != nil {
		return types.NetworkCreateResponse{}, err
	}

	networkResource, err := cli.NetworkCreate(context.Background(), networkName, types.NetworkCreate{})
	return networkResource, err
}
func (receiver *dockerNetwork) Remove(networkID string) error {
	cli, err := Docker.NewClient()
	if err != nil {
		return err
	}

	err = cli.NetworkRemove(context.Background(), networkID)
	return err
}

func (receiver *dockerNetwork) List() ([]types.NetworkResource, error) {
	cli, err := Docker.NewClient()
	if err != nil {
		return nil, err
	}

	networkResources, err := cli.NetworkList(context.Background(), types.NetworkListOptions{})
	return networkResources, err
}
func (receiver *dockerNetwork) Prune() (types.NetworksPruneReport, error) {
	cli, err := Docker.NewClient()
	if err != nil {
		return types.NetworksPruneReport{}, err
	}

	report, err := cli.NetworksPrune(context.Background(), filters.Args{})
	return report, err
}

func (receiver *dockerNetwork) Inspect(networkID string) (types.NetworkResource, error) {
	cli, err := Docker.NewClient()
	if err != nil {
		return types.NetworkResource{}, err
	}

	networkResource, err := cli.NetworkInspect(context.Background(), networkID, types.NetworkInspectOptions{})
	return networkResource, err
}
func (receiver *dockerNetwork) InspectWithRaw(networkID string) (types.NetworkResource, []byte, error) {
	cli, err := Docker.NewClient()
	if err != nil {
		return types.NetworkResource{}, []byte{}, err
	}

	networkResource, raw, err := cli.NetworkInspectWithRaw(context.Background(), networkID, types.NetworkInspectOptions{})
	return networkResource, raw, err
}

func (receiver *dockerNetwork) Connect(networkId, containerId string) error {
	cli, err := Docker.NewClient()
	if err != nil {
		return err
	}

	err = cli.NetworkConnect(context.Background(), networkId, containerId, &network.EndpointSettings{})
	return err
}
func (receiver *dockerNetwork) Disconnect(cli *client.Client, networkId, containerId string) error {
	cli, err := Docker.NewClient()
	if err != nil {
		return err
	}

	err = cli.NetworkDisconnect(context.Background(), networkId, containerId, true)
	return err
}
